<template>
  <div class="side-wrapper">
    <logo :collapse="collapsed" />
    <scroll-bar class="side-main">
      <a-menu
        :mode="mode"
        :inline-collapsed="!collapsed"
        theme="dark"
        :selectedKeys="[$route.path]"
        :open-keys="openKeys"
        @openChange="changeOpen"
      >
        <template v-for="(item, index) in menuRoutes">
          <menu-item 
            v-if="!item.children && !item.hidden" 
            :key="item.path" 
            :currentRoute="item" 
          />
          <sub-menu 
            v-else-if="!item.hidden"
            :key="'group-' + index"
            :currentRoute="item"
          />
        </template>
      </a-menu>
    </scroll-bar>
  </div>
</template>

<script>
import logo from './logo';
import subMenu from './subMenu';
import menuItem from './menuItem';
import { mapGetters } from 'vuex';
export default {
  name: 'sideBar',
  props: {
    collapsed: {
      type: Boolean,
      default: true
    },
    mode: {
      type: String,
      default: 'inline'
    }
  },
  components: { logo, subMenu, menuItem },
  data() {
    return {
      openKeys: []
    };
  },
  computed: {
    ...mapGetters(['baseRoute']),
    menuRoutes() {
      return this.baseRoute.filter(route => !route.hidden);
    }
  },
  watch: {
    $route: {
      handler(route) {
        if (route.matched && route.matched.length > 1) {
          this.openKeys = route.matched.map(item => item.path);
        }
      },
      immediate: true
    }
  },
  methods: {
    changeOpen(keys) {
      this.openKeys = keys;
    }
  }
};
</script>
